package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.minio.MinIOService;
import com.heima.common.util.MediaThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-06-22
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {
    @Autowired
    MinIOService minIOService;

    /**
     * 文件上传
     * @param file
     * @return
     */
    @Override
    public ResponseResult upload(MultipartFile file) {
        //从线程中获取userid
        User user = MediaThreadLocalUtil.get();
        if(user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        String url = minIOService.upload(file);
        WmMaterial material=new WmMaterial();
        material.setUserId(user.getUserId());
        material.setCreatedTime(new Date());
        material.setUrl(url);
        material.setType(0);
        material.setIsCollection(false);
        this.save(material);
        return ResponseResult.okResult(material);
    }

    @Override
    public ResponseResult listByCollection(WmMaterialDto materialDto) {
        User user = MediaThreadLocalUtil.get();
        if(user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        IPage<WmMaterial> page=new Page<>(materialDto.getPage(),materialDto.getSize());

        LambdaQueryWrapper<WmMaterial> query=new LambdaQueryWrapper<>();
        if(materialDto.getIsCollection()!=null){
            query.eq(WmMaterial::getIsCollection,materialDto.getIsCollection());
        }
        query.eq(WmMaterial::getUserId,user.getUserId());
        query.orderByDesc(WmMaterial::getCreatedTime);
        IPage<WmMaterial> iPage = this.page(page, query);
        ResponseResult result=new PageResponseResult(materialDto.getPage(),materialDto.getSize(),iPage.getTotal(),iPage.getRecords());
        return result;
    }



    @Override
    public ResponseResult updateCollect(Integer id) {
        WmMaterial wmMaterial = this.getById(id);
        wmMaterial.setIsCollection(true);
        this.updateById(wmMaterial);
        return ResponseResult.okResult();
    }


}
